package org.jetlinks.community.codec;


import lombok.NoArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.hswebframework.ezorm.rdb.codec.DateTimeCodec;

import java.util.Date;

/**
 * 日期转换器
 */
@NoArgsConstructor
public class EnhanceDateTimeCodec extends DateTimeCodec {


    /**
     * Instantiates a new Enhance date time codec.
     *
     * @param format the format
     * @param toType the to type
     */
    public EnhanceDateTimeCodec(String format, Class toType) {
        super(format, toType);
    }


    @Override
    public Object encode(Object value) {
        if (value instanceof String && NumberUtils.isCreatable((String) value)) {
            return new Date(NumberUtils.createNumber(value.toString()).longValue());
        }
        return super.encode(value);
    }
}
